﻿using Microsoft.Extensions.Configuration;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
//using JConfig;
using Microsoft.Extensions.DependencyInjection;
using Common.Cache;

namespace Common.UTest
{
    public abstract class UnitTestBase
    {
        protected IConfiguration Configuration { get; private set; }
        protected IServiceProvider ServiceProvider { get; private set; }
        [SetUp]
        public virtual void Initialize()
        {
            //InitConfiguration();
            InitServiceCollection();
            SetUp();
        }
        //private void InitConfiguration()
        //{
        //    this.Configuration = new ConfigurationBuilder()
        //        .SetBasePath(AppContext.BaseDirectory)
        //        .AddJsonFile("appsettings.json", true, true)
        //        .Add(GetJConfig())
        //        .Build();
        //}
        //private IConfigurationSource GetJConfig()
        //{
        //    JConfigClientAccount account = new JConfigClientAccount
        //    {
        //        AppId = "15031459716940800",
        //        Secret = "27cde404c1f044d99545ba7085f81861",
        //        Env = "dev",
        //        Hosts = new string[] { "http://jconfig.zys6d.cn" },
        //        NodeId = null
        //    };
        //    AppConfiguration.Initialize(account);
        //    JConfigurationSource source = new JConfigurationSource();
        //    source.Account = account;
        //    return source;
        //}

        private void InitServiceCollection()
        {
            ServiceCollection services = new ServiceCollection();
            services.AddSingleton(this.Configuration);
            services.AddRedisCache(this.Configuration);
            ConfigureServices(services);
            this.ServiceProvider = services.BuildServiceProvider();
        }
        protected virtual void ConfigureServices(IServiceCollection services)
        {

        }
        protected virtual void SetUp() { }
    }
}
